package com.baidu.shoppingmall.auth.controller;

import com.baidu.shoppingmall.auth.domain.AuthorityDTO;
import com.baidu.shoppingmall.auth.domain.AuthorityVO;
import com.baidu.shoppingmall.auth.service.AuthorityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yjf
 */
@RestController
@RequestMapping("/auth")
public class AuthorityController {
    private static final Logger logger = LoggerFactory.getLogger(AuthorityController.class);

    @Autowired
    private AuthorityService authorityService;

    /**
     * 查询根权限
     * @return 根权限集合
     */
    @GetMapping("/authority/root")
    public List<AuthorityVO> listRootPriorities() {
        List<AuthorityDTO> AuthorityDTOs = authorityService.listRootAuthorities();

        List<AuthorityVO> AuthorityVOs = new ArrayList<>(AuthorityDTOs.size());
        //把DTO的数据拷贝到VO中
        for(AuthorityDTO AuthorityDTO : AuthorityDTOs) {
            AuthorityVOs.add(AuthorityDTO.clone(AuthorityVO.class));
        }

        return AuthorityVOs;
    }
    /**
     * 根据父权限id查询子权限
     *
     * @param parentId 父权限id
     * @return 子权限
     */
    @GetMapping("/child/{parentId}")
    public List<AuthorityVO> listChildAuthorities(@PathVariable("parentId") Long parentId) {
        try {
            List<AuthorityDTO> authorityDTOs = authorityService.listChildAuthorities(parentId);
            if (authorityDTOs == null) {
                authorityDTOs = new ArrayList<>();
            }

            List<AuthorityVO> authorityVOs = new ArrayList<>(authorityDTOs.size());
            for (AuthorityDTO authorityDTO : authorityDTOs) {
                authorityVOs.add(authorityDTO.clone(AuthorityVO.class));
            }

            return authorityVOs;
        } catch (Exception e) {
            logger.error("error", e);
        }
        return new ArrayList<>();
    }

    /**
     * 根据id查询权限
     *
     * @param id 权限id
     * @return 权限
     */
    @GetMapping("/{id}")
    public AuthorityVO getAuthorityById(@PathVariable("id") Long id) {
        try {
            AuthorityDTO authorityDTO = authorityService.getAuthorityById(id);
            if (authorityDTO == null) {
                authorityDTO = new AuthorityDTO();
            }

            return authorityDTO.clone(AuthorityVO.class);
        } catch (Exception e) {
            logger.error("error", e);
        }
        return new AuthorityVO();
    }

    /**
     * 新增权限
     *
     * @param authorityVO 权限VO对象
     * @return 新增成功的记录条数
     */
    @PostMapping("/")
    public Integer saveAuthority(@RequestBody AuthorityVO authorityVO) {
        Integer count = 0;
        try {
            AuthorityDTO authorityDTO = authorityVO.clone(AuthorityDTO.class);
            count = authorityService.saveAuthority(authorityDTO);
        } catch (Exception e) {
            logger.error("error", e);
            return count;
        }
        return count;
    }

    /**
     * 更新权限
     *
     * @param authorityVO 权限PO对象
     * @return 更新成功的记录条数
     */
    @PutMapping("/")
    public Integer updateAuthority(@RequestBody AuthorityVO authorityVO) {
        Integer count = 0;
        try {
            AuthorityDTO priorityDTO = authorityVO.clone(AuthorityDTO.class);
            count = authorityService.updateAuthority(priorityDTO);
        } catch (Exception e) {
            logger.error("error", e);
            return count;
        }
        return count;
    }
    /**
     * 根据前台点删除权限获取的ID递归删除权限信息
     * 在删除前先用递归的方式判断他最底层的子节点是否被占用（true不是被占用，false反之），一步一步向上判断(通过这个CheckRolePermissionRelationVisitor里面的visit方法)
     * 然后通过get方法获取里面的返回值（status）
     * 在判断，如果没有占用就通过递归删除，调用DeletePermissionVisitor里面的visit方法
     * @param
     */
    @DeleteMapping("/delete")
    public String delete(Integer id) {
        Integer count = authorityService.delete(id);
       if (count==0){
           return "删除失败";
       }
           return "删除成功";
    }
}

